<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Zend
 * @package    Admin_Model
 * @subpackage Order
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * The domain for order
 * 
 * @category   Zend
 * @package    Admin_Model
 * @subpackage Order
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Admin_Model_Order extends Zend_Db_Table_Abstract
{

    /**
     * The name of table
     * @var string 
     */
    protected $_name = 'orders';

    /**
     * Save order
     * @param array $row
     */
    public function save($row)
    {
        // check ID (insert or update)
        $id = isset($row['id']) ? $row['id'] : null;
        if ($id) {
            unset($row['id']);
            $this->update($row, array('id = ?' => $id));
        } else {
            $row['created'] = new Zend_Db_Expr('NOW()');
            $this->insert($row);
        }
    }

    /**
     * Get orders by filter 
     * @return array 
     */
    public function getAllByFilter($id, $customerName, $billToName, $shipToName, $totalFrom, $totalTo, $updatedFrom, $updatedTo, $createdFrom, $createdTo, $orderStatusId, $sort, $direction, $page, $rowCount)
    {
        $query = $this->getAdapter()->select();
        $query->from(array('o' => $this->_name), array('o.id', 'o.total', 'o.updated', 'o.created'));
        $query->join(array('os' => 'order_status'), 'o.order_status_id = os.id', 'os.name as order_status_name');
        $query->join(array('c' => 'customer'), 'o.customer_id = c.id', 'c.full_name as customer_name');
        $query->joinLeft(array('i' => 'invoice'), 'o.id = i.order_id', 'i.to_name as bill_to_name');
        $query->joinLeft(array('s' => 'shipment'), 'o.id = s.order_id', 's.to_name as ship_to_name');

        // Check filters
        if ($id) {
            $query->where('o.id = ?', $id);
        }
        if ($customerName) {
            $query->where('c.full_name LIKE ?', $customerName . '%');
        }
        if ($billToName) {
            $query->where('i.to_name LIKE ?', $billToName . '%');
        }
        if ($shipToName) {
            $query->where('s.to_name LIKE ?', $shipToName . '%');
        }
        if ($totalFrom) {
            $query->where('o.total >= ?', $totalFrom);
        }
        if ($totalTo) {
            $query->where('o.total <= ?', $totalTo);
        }
        if ($updatedFrom) {
            $query->where('o.updated >= ?', $updatedFrom);
        }
        if ($updatedTo) {
            $query->where('o.updated <= ?', $updatedTo);
        }
        if ($createdFrom) {
            $query->where('o.created >= ?', $createdFrom);
        }
        if ($createdTo) {
            $query->where('o.created <= ?', $createdTo);
        }
        if ($orderStatusId) {
            $query->where('o.order_status_id = ?', $orderStatusId);
        }

        if ($sort && $direction) {
            $query->order($sort . ' ' . $direction);
        }

        $query->limitPage($page, $rowCount);

        // Get orders from DB
        $result = $this->getAdapter()->fetchAll($query, null, Zend_Db::FETCH_OBJ);
        return $result;
    }

    /**
     * Get count of orders by filter 
     * @return integer 
     */
    public function getCountByFilter($id, $customerName, $billToName, $shipToName, $totalFrom, $totalTo, $updatedFrom, $updatedTo, $createdFrom, $createdTo, $orderStatusId)
    {
        $query = $this->getAdapter()->select();
        $query->from(array('o' => $this->_name), new Zend_Db_Expr('COUNT(*)'));
        $query->join(array('os' => 'order_status'), 'o.order_status_id = os.id', '');
        $query->join(array('c' => 'customer'), 'o.customer_id = c.id', '');
        $query->joinLeft(array('i' => 'invoice'), 'o.id = i.order_id', '');
        $query->joinLeft(array('s' => 'shipment'), 'o.id = s.order_id', '');

        // Check filters
        if ($id) {
            $query->where('o.id = ?', $id);
        }
        if ($customerName) {
            $query->where('c.full_name LIKE ?', $customerName . '%');
        }
        if ($billToName) {
            $query->where('i.to_name LIKE ?', $billToName . '%');
        }
        if ($shipToName) {
            $query->where('s.to_name LIKE ?', $shipToName . '%');
        }
        if ($totalFrom) {
            $query->where('o.total >= ?', $totalFrom);
        }
        if ($totalTo) {
            $query->where('o.total <= ?', $totalTo);
        }
        if ($updatedFrom) {
            $query->where('o.updated >= ?', $updatedFrom);
        }
        if ($updatedTo) {
            $query->where('o.updated <= ?', $updatedTo);
        }
        if ($createdFrom) {
            $query->where('o.created >= ?', $createdFrom);
        }
        if ($createdTo) {
            $query->where('o.created <= ?', $createdTo);
        }
        if ($orderStatusId) {
            $query->where('o.order_status_id = ?', $orderStatusId);
        }

        // Get count of orders from DB
        $result = $this->getAdapter()->fetchOne($query);

        return $result;
    }

    /**
     * Get order by ID
     * @param integer $orderId
     * @return array 
     */
    public function getById($orderId)
    {
        $query = $this->getAdapter()->select();
        $query->from(array('o' => $this->_name), array('o.id', 'o.total', 'o.updated', 'o.created', 'o.customer_id', 'o.order_status_id'));
        $query->join(array('os' => 'order_status'), 'o.order_status_id = os.id', 'os.name as order_status_name');
        $query->where('o.id = ?', $orderId);

        // Get order from DB
        $result = $this->getAdapter()->fetchRow($query, null, Zend_Db::FETCH_OBJ);
        return $result;
    }

}
